package com.huaxindata.im.model.utils;

import com.huaxin.common.tools.Log;
import com.huaxindata.im.model.entity.ChatFileMessageBody;
import com.huaxindata.im.model.entity.ChatImageMessageBody;
import com.huaxindata.im.model.entity.ChatLocationMessageBody;
import com.huaxindata.im.model.entity.ChatMessage;
import com.huaxindata.im.model.entity.ChatMessageBody;
import com.huaxindata.im.model.entity.ChatNewsMessageBody;
import com.huaxindata.im.model.entity.ChatStationMessageBody;
import com.huaxindata.im.model.entity.ChatSvgMessageBody;
import com.huaxindata.im.model.entity.ChatSysMessageBody;
import com.huaxindata.im.model.entity.ChatTargetMessageBody;
import com.huaxindata.im.model.entity.ChatTextMessageBody;
import com.huaxindata.im.model.entity.ChatVideoMessageBody;
import com.huaxindata.im.model.entity.ChatVoiceMessageBody;
import com.huaxindata.im.model.entity.ChatWarnMessageBody;
import com.huaxindata.im.model.entity.ChatWorktaskMessageBody;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;

/**
 * @author lwjing
 * @date 2018/1/11
 * @Desc 处理各类型消息的发送
 * Copyright HUAXIN DATA. All rights reserved.
 */
public class SendPlayload {
    //magic数，扩展2为0占8个字节
    private static final byte[] MAGIC           = {0x68, 0x75, 0x61, 0x78, 0x69, 0x6E, 0x00, 0x00};//huaxin
    // MAGIC（8个字节） + 消息类型（2个字节） +  发送人的字节长度（1个字节） + 接收人的字节长度（1个字节）+ 消息文本的长度(1 - 1000)(4个字节) + 发送时间(8个字节) + 消息文本
    private static final int    PLAYLOAD_LENGTH = 24;

    //消息类型 1个字节
    private static class MessageType {
        public static final byte[] SYSTEM = {0x00, 0x21};//系统消息--33

        //public static final byte[] GROUP = {0x00, 0x20};//群发消息--32

        public static final byte[] TEXT     = {0x00, 0x00};//文本消息--0
        public static final byte[] IMAGE    = {0x00, 0x01};//图片消息--1
        public static final byte[] LOCATION = {0x00, 0x02};//位置消息--2
        public static final byte[] VOICE    = {0x00, 0x03};//声音消息--3
        public static final byte[] VIDEO    = {0x00, 0x04};//视频消息--4
        public static final byte[] FILE     = {0x00, 0x05};//文件消息--5

        public static final byte[] VOICE_CALL = {0x00, 0x06};//语音呼叫--6

        //expand
        public static final byte[] STATION  = {0x00, 0x10};//发送站点--16
        public static final byte[] SVG      = {0x00, 0x11};//发送工艺图--17
        public static final byte[] WARN     = {0x00, 0x12};//发送告警--18
        public static final byte[] WORKTASK = {0x00, 0x13};//发送工单--19
        public static final byte[] DATAITEM = {0x00, 0x14};//发送指标--20
    }

    /**
     * 根据 ChatMessage 对象构造消息发送需要的byte[]
     *
     * @param message
     * @return
     * @throws UnsupportedEncodingException
     */
    public static byte[] createData(ChatMessage message) throws UnsupportedEncodingException {

        //对messageBody进行字段拼接
        String content = matchContent(message);
        message.message=content;
        final ByteBuffer buffer = ByteBuffer.allocate(PLAYLOAD_LENGTH + message.getByteLength(content) + 2);
        //魔法数 8
        buffer.put(MAGIC);
        //类型 2
        switch (message.type) {
            case ChatMessageBody.Type.SYSTEM:
                buffer.put(MessageType.SYSTEM);
                break;
            case ChatMessageBody.Type.VOICE_CALL:
                buffer.put(MessageType.VOICE_CALL);
                break;
            case ChatMessageBody.Type.WORKTASK:
                buffer.put(MessageType.WORKTASK);
                break;
            case ChatMessageBody.Type.TARGET:
                buffer.put(MessageType.DATAITEM);
                break;
            case ChatMessageBody.Type.STATION:
                buffer.put(MessageType.STATION);
                break;
            case ChatMessageBody.Type.SVG:
                buffer.put(MessageType.SVG);
                break;
            case ChatMessageBody.Type.WARN:
                buffer.put(MessageType.WARN);
                break;
            case ChatMessageBody.Type.FILE:
                buffer.put(MessageType.FILE);
                break;
            case ChatMessageBody.Type.VIDEO:
                buffer.put(MessageType.VIDEO);
                break;
            case ChatMessageBody.Type.VOICE:
                buffer.put(MessageType.VOICE);
                break;
            case ChatMessageBody.Type.LOCATION:
                buffer.put(MessageType.LOCATION);
                break;
            case ChatMessageBody.Type.IMAGE:
                buffer.put(MessageType.IMAGE);
                break;
            default:
                buffer.put(MessageType.TEXT);
                break;
        }
        //发送人 1
        buffer.put((byte) ((message.sender.getBytes().length & 0x000000FF)));
        //接收人 1
        buffer.put((byte) ((message.receiver.getBytes().length & 0x000000FF)));
        //消息内容长度4
        buffer.put(intToBytes(content.getBytes("UTF-8").length));
        //发送时间 8
        buffer.put(long2bytes(message.timestamp));
        //内容
        buffer.put((message.getAllMessage(content)).getBytes("UTF-8"));

        buffer.flip();//将当前写入数据的position置0
        final byte[] data = new byte[buffer.remaining()];//buffer.remaining()用于得到写入数据的大小
        buffer.get(data);//获取buffer中的数据，存入到data中

        return data;
    }

    public static String matchContent(ChatMessage message) {
        switch (message.type) {
            case ChatMessageBody.Type.TEXT://文本
                ChatTextMessageBody chatTextMessageBody = (ChatTextMessageBody) message.getMessageBody();
                return chatTextMessageBody.content;

            case ChatMessageBody.Type.IMAGE://图片
                ChatImageMessageBody chatImageMessageBody = (ChatImageMessageBody) message.getMessageBody();
                return chatImageMessageBody.matchMessage();

            case ChatMessageBody.Type.LOCATION://位置
                ChatLocationMessageBody chatLocationMessageBody = (ChatLocationMessageBody) message.getMessageBody();
                return chatLocationMessageBody.matchLocationMessage();

            case ChatMessageBody.Type.VOICE://语音
                ChatVoiceMessageBody chatVoiceMessageBody = (ChatVoiceMessageBody) message.getMessageBody();
                return chatVoiceMessageBody.matchMessage();

            case ChatMessageBody.Type.FILE://文件
                ChatFileMessageBody chatFileMessageBody = (ChatFileMessageBody) message.getMessageBody();
                return chatFileMessageBody.matchMessage();

            case ChatMessageBody.Type.VIDEO://短视频
                ChatVideoMessageBody chatVideoMessageBody = (ChatVideoMessageBody) message.getMessageBody();
                return chatVideoMessageBody.matchMessage();

            case ChatMessageBody.Type.STATION://站点
                ChatStationMessageBody chatStationMessageBody = (ChatStationMessageBody) message.getMessageBody();
                Log.e("lwjing", "match STATION :" + chatStationMessageBody.matchStationMessage());
                return chatStationMessageBody.matchStationMessage();

            case ChatMessageBody.Type.SVG://Svg
                ChatSvgMessageBody chatSvgMessageBody = (ChatSvgMessageBody) message.getMessageBody();
                Log.e("lwjing", "match SVG :" + chatSvgMessageBody.matchSvgMessage());
                return chatSvgMessageBody.matchSvgMessage();

            case ChatMessageBody.Type.WARN://告警
                ChatWarnMessageBody chatWarnMessageBody = (ChatWarnMessageBody) message.getMessageBody();
                Log.e("lwjing", "match WARN :" + chatWarnMessageBody.matchWarnMessage());
                return chatWarnMessageBody.matchWarnMessage();

            case ChatMessageBody.Type.WORKTASK://工单
                ChatWorktaskMessageBody chatWorktaskMessageBody = (ChatWorktaskMessageBody) message.getMessageBody();
                Log.e("lwjing", "match WORKTASK :" + chatWorktaskMessageBody.content);
                return chatWorktaskMessageBody.content;

            case ChatMessageBody.Type.TARGET://指标
                ChatTargetMessageBody chatTargetMessageBody = (ChatTargetMessageBody) message.getMessageBody();
                Log.e("lwjing", "match TARGET :" + chatTargetMessageBody.content);
                return chatTargetMessageBody.content;

            case ChatMessageBody.Type.NEWS://新闻
                ChatNewsMessageBody chatNewsMessageBody = (ChatNewsMessageBody) message.getMessageBody();
                Log.e("lwjing", "match NEWS :" + chatNewsMessageBody.content);
                return chatNewsMessageBody.content;

            case ChatMessageBody.Type.SYSTEM://系统消息
                ChatSysMessageBody chatSysMessageBody= (ChatSysMessageBody) message.getMessageBody();
                return chatSysMessageBody.content;
            default:
                return "";
        }
    }

    private static byte[] intToBytes(int value) {
        byte[] byte_src = new byte[4];
        byte_src[3] = (byte) ((value & 0xFF000000) >> 24);
        byte_src[2] = (byte) ((value & 0x00FF0000) >> 16);
        byte_src[1] = (byte) ((value & 0x0000FF00) >> 8);
        byte_src[0] = (byte) ((value & 0x000000FF));
        return byte_src;
    }

    private static byte[] long2bytes(long num) {
        byte[] b = new byte[8];
        for (int i = 0; i < 8; i++) {
            b[i] = (byte) (num >>> (56 - (i * 8)));
        }
        return b;
    }
}


